package com.hm.cloud.nacos;

import com.hm.cloud.nacos.feign.TestFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author huangmai
 * @Title: Comsumer
 * @ProjectName spring-cloud-alibaba
 * @Description: TODO
 * @date 2021/10/1217:13
 */

@Slf4j
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

    @Autowired
    private TestFeignClient testFeignClient;

    /**
     * 创建服务调用的对象
     *
     * @return
     */
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RestController
    public class ConsumerController {
        @Autowired
        private LoadBalancerClient client;
        @Autowired
        private RestTemplate restTemplate;

        @GetMapping("/consumer")
        public String consumer() {
            log.info("---------消费者开始------------");
            //获取服务对象，注意nacos-provider是服务提供者应用名称
            ServiceInstance instance = client.choose("nacos-provider");
            //调用服务
            String result = restTemplate.getForObject(instance.getUri() + "/provider", String.class);
            log.info("---------消费者结束--------result{}----", result);
            return result;
        }

        @GetMapping("/testFeign")
        public Integer testFeign(){
            Integer integer = testFeignClient.testFeign(10086);
            return integer;
        }
    }
}
